/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.ruoyi.project.module.api.common.utils;

import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回数据
 *
 * @author Mark sunlightcs@gmail.com
 */
public class ApiResult extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	
	public ApiResult() {
		put("code", 0);
		put("msg", "success");
	}
	
	public static ApiResult error() {
		return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
	}
	
	public static ApiResult error(String msg) {
		return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
	}
	
	public static ApiResult error(int code, String msg) {
		ApiResult apiResult = new ApiResult();
		apiResult.put("code", code);
		apiResult.put("msg", msg);
		return apiResult;
	}

	public static ApiResult ok(String msg) {
		ApiResult apiResult = new ApiResult();
		apiResult.put("msg", msg);
		return apiResult;
	}
	
	public static ApiResult ok(Map<String, Object> map) {
		ApiResult apiResult = new ApiResult();
		apiResult.putAll(map);
		return apiResult;
	}
	
	public static ApiResult ok() {
		return new ApiResult();
	}

	public ApiResult put(String key, Object value) {
		super.put(key, value);
		return this;
	}
}
